function replaceAll(string, token, newtoken) {
    while (string.search(token) != -1) {
	string = string.replace(token, newtoken);
    }
    return string;
}

function slugify(value){
    value = value.toLowerCase();
    value = replaceAll(value, /á|à|ã|â|ä/, 'a');
    value = replaceAll(value, /é|è|ẽ|ê|ë/, 'e');
    value = replaceAll(value, /í|ì|ĩ|î|ï/, 'i');
    value = replaceAll(value, /ó|ò|õ|ô|ö/, 'o');
    value = replaceAll(value, /ú|ù|ũ|û|ü/, 'u');
    value = replaceAll(value, /ç|ĉ/, 'c');
    value = replaceAll(value, ' ', '-');
    return replaceAll(value, /[^a-z0-9\_\-]/, '');
}

function update_slug(field_id, slug_id){
    var name_field = $(field_id);
    var slug_field = $(slug_id);
    
    var fnc = function(event){
        var target = $(event.target);
        var value = target.getProperty('value').clean().toLowerCase();
        slug_field.setProperty('value', slugify(value));
    }
    
    name_field.addEvent('keyup', fnc);
    name_field.addEvent('blur', fnc);
}

function db_message(msg){
	var db_msg = new Element('div', {'class':'db_msg'});
	var db_content = new Element('div', {'class':'content'});
	db_content.inject(db_msg);
	db_content.set('text', msg);
	db_msg.inject(document.body);
}
